package cn.com.aiouyi.controller.admin;

import cn.com.aiouyi.common.Message;
import cn.com.aiouyi.entity.FileClass;
import cn.com.aiouyi.entity.Member;
import cn.com.aiouyi.page.Pageable;
import cn.com.aiouyi.service.FileClassService;
import cn.com.aiouyi.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by tommy on 2018/4/26.
 */
@Controller("adminFileClassController")
@RequestMapping("/admin/fileClass")
public class FileClassController extends BaseController {

    @Resource(name = "fileClassServiceImpl")
    private FileClassService fileClassService;

    @Resource(name = "memberServiceImpl")
    private MemberService memberService;

    @GetMapping("/list")
    public String list(Pageable pageable, ModelMap model) {

        model.put("page", fileClassService.findPage(pageable));
        return "/admin/fileClass/list";
    }

    @GetMapping("/add")
    public String add() {

        return "/admin/fileClass/add";
    }

    @PostMapping("/save")
    public String save(@Valid FileClass fileClass, RedirectAttributes redirectAttributes) {
        fileClassService.save(fileClass);
        addFlashMessage(redirectAttributes, Message.SUCCESS);
        return "redirect:list";
    }

    @GetMapping("/edit")
    public String edit(Long id, ModelMap model) {

        model.put("fileClass", fileClassService.find(id));
        return "/admin/fileClass/edit";
    }

    @PostMapping("/update")
    public String update(@Valid FileClass fileClass,
                         ModelMap model, RedirectAttributes redirectAttributes) {
        fileClassService.update(fileClass);
        addFlashMessage(redirectAttributes, Message.SUCCESS);
        return "redirect:list";
    }


    @PostMapping("/delete")
    @ResponseBody
    public Message delete(Long[] ids, HttpServletRequest request, Model model){
        if (ids != null && ids.length >0){
            fileClassService.delete(ids);
            return Message.SUCCESS;
        } else {
            model.addAttribute(ERROR_VIEW_MESSAGE_KEY, "id不能为空");
            return Message.ERROR;
        }
    }
}
